﻿//Copyright (c) Microsoft Corporation.  All rights reserved.
//#define VERBOSE_SCREEN_TRACE
using System;
using System.Collections.Generic;

namespace Microsoft.Ted.Wacel.Base
{
    public abstract class CloudDataStructure
    {
        protected IDataProvider MyDataProvider { get; private set; }
        protected ILockProvider MyLockProvider { get; private set; }
        
        public string Name { get; private set; }
      
        public CloudDataStructure(string name, IDataProvider dataProvider, ILockProvider lockProvider)
        {
            MyDataProvider = dataProvider;
            MyLockProvider = lockProvider;
            Name = name;
        }

        protected void PutInSub(string key, long value)
        {
            MyLockProvider.PutInSub(key, value);
        }
        public void ProviderUpdate(string key, object value)
        {
            MyDataProvider.Update(key, value);
        }
        protected void Put(string key, object value, string partition = null)
        {
#if VERBOSE_SCREEN_TRACE
            Console.Write(key + ":" + (value is NodeData ? ((NodeData)value).Value.ToString() : value.ToString())+ "\t");
#endif
            MyDataProvider.Put(key, value, partition);
        }
        protected void BulkPut(IEnumerable<KeyValuePair<string,object>> values, string partition = null)
        {
            MyDataProvider.BulkPut(values, partition);
        }
        protected object Get(string key, string partition = null, Type typeHint = null, Type typeHint2 = null)
        {
            return MyDataProvider.Get(key, partition, typeHint, typeHint2);
        }
        protected IEnumerable<KeyValuePair<string, object>> BulkGet(List<string> keys, string partition = null, Type typeHint = null, Type typeHint2 = null)
        {
            return MyDataProvider.BulkGet(keys, partition, typeHint, typeHint2);
        }
        protected long InterlockedRead(string key, long defaultValue)
        {
            return Increment(key, 0, defaultValue);
        }
        protected long Increment(string key, long increment, long defaultValue)
        {
            return MyLockProvider.Increment(key, increment, defaultValue);
        }
        protected long ReadAndLock(string key, long defaultValue, TimeSpan timespan, out object lockHandle)
        {
            return MyLockProvider.ReadAndLock(key, defaultValue, timespan, out lockHandle);
        }
        protected void PutAndUnlock(string key, long value, object lockHandle)
        {
            MyLockProvider.PutAndUnlock(key, value, lockHandle);
        }
        protected void Unlock(string key, object lockHandle)
        {
            MyLockProvider.Unlock(key, lockHandle);
        }
      
        public void PutTag<T>(string key, T value)
        {
            Put(key, value);
        }
        public T GetTag<T>(string key, T defaultValue)
        {
            var value = Get(key);
            if (value != null)
                return (T)value;
            else
                return defaultValue;
        }
        protected void ProviderDelete(string key, string partition=null)
        {
            MyDataProvider.Delete(key, partition);
        }
        protected IEnumerable<KeyValuePair<string, object>> ProviderGetAll(string partition = null, Type typeHint = null, Type typeHint2 = null)
        {
            return MyDataProvider.GetAll(partition, typeHint, typeHint2);
        }
        protected void ProviderClear()
        {
            MyDataProvider.Clear();
        }
    }
}
